<?php namespace App\Http\Controllers\admin\development;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Input;
use App\Models\DevelopmentUser;
use App\Models\Users;
use App\Models\News;
use Redirect;
use Session;


class DevelopmentController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */

	public function developmentList()
	{
		$current = (int)Input::get("current");
		$rowCount = (int)Input::get("rowCount");
		$sort = Input::get("sort")["id"];
		$sort = $sort ? $sort : "desc";

		$model = News::with(["development"]);
		$count = $model->get()->count();

		$data = $model->skip(($current - 1)* $rowCount)->take($rowCount)->get()->toArray();

		return array(
			"current" => $current,
			"rowCount" => $rowCount,
			"rows" => $data,
			"total" => $count
			);
	}
	
	public function index()
	{
		//
	  
	    if(Session::has('admin')){
		   return view("admin.development.index")->withIsDevelopment(true);
		}else{
			return Redirect::to('admin/dologin');
		}
	}

	public function addDevelopment(){
		return view("admin.development.add_development")->withIsDevelopment(true);;
	}

	public function editNews()
	{
		return view("admin.development.edit_news")->withIsDevelopment(true);;
	}

	public function addUser()
	{
		$data = Input::all();
		$user = new Users;
		$user->role_id = env("DEVELOPMENT");
		$user->save();
		$data["uid"] = $user->id;
		$imgArr = Input::only("image");
		$this->moveImages($imgArr, env("OTHERS_DIR"), $user->id);
		DevelopmentUser::create($data);
		return $this->json([]);
	}

	public function getDevelopmentUser()
	{
		$data = DevelopmentUser::all()->toArray();
		return $this->json(["data" => $data]);
	}

	public function addNews()
	{
		return view("admin.development.add_news")->withIsDevelopment(true);;
	}

	public function saveNews()
	{
		$data = Input::all();
		News::create($data);
		return $this->json([]);
	}

	public function getNews()
	{
		$nid = Input::get("nid");
		$model = News::with(["development"])->where("id", $nid);
		if($model->count())
		{
			$data = $model->get()[0]->toArray();
			return $this->json(["data" => $data]);
		}

	}

	public function updateNews()
	{
		$nid = Input::get("nid");
		$model = News::where("id", $nid);
		if($model->count())
		{
			$data = Input::except("nid");
			$model->update($data);
			return $this->json([]);
		}
	}

	public function deleteNews()
	{
		$nid = Input::get("nid");
		$model = News::where("id", $nid);
		if($model->count())
		{
			$model->delete();
			return $this->json([]);
		}
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
